//
//  ContactvcCell.m
//  YZZX
//
//  Created by Easaa on 15/6/3.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ContactvcCell.h"
#import "ContractView.h"


@implementation ContactvcCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(id)initWithDataStyle:(UITableViewCellStyle)stylessss reuseIdentifier:(NSString *)nssstr
{
    self = [super initWithStyle:stylessss reuseIdentifier:nssstr];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    
    return self;
}



- (void)configureCellWithAttributes:(NSDictionary *)att
{
    
    NSDictionary *queto = att[@"quote"];
    if ([queto isEqual:[NSNull null]]){
        [self creatSingleComment:att];
    } else {
        [self creatMoreComment:att];
    }
   
    
//    
//     ContractView *view = [[ContractView alloc]initWithFrame:CGRectMake(0, 0, screenW,height + 15)];
//    
//    
//    
//    
//    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
//    
//    CGFloat height = [self.class heightOfCommentWithAttributes:att withLastHeight:0 lastWidth:screenW];
//    
//    ContractView *view = [[ContractView alloc]initWithFrame:CGRectMake(0, 0, screenW,height + 15)];
//    
//    [self addCommentViewWith:att view:view isFirst:YES];
//    [self.contentView addSubview:view];
}


- (void)creatSingleComment:(NSDictionary *)dic
{
   
    
    CGFloat  height = [self.class heightWithConstraintWidth:Dev_ScreenWidth-40 string:dic[@"comment"]];
    
     ContractView *view = [[ContractView alloc]initWithFrame:CGRectMake(10, 0, Dev_ScreenWidth-20,height+40 )];
     view.attrib =dic;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCommentView:)];
    [view addGestureRecognizer:tap];

    
    UILabel *contextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,30 , Dev_ScreenWidth-40, height)];
    contextLabel.text = dic[@"comment"];
    contextLabel.numberOfLines = 0;
    contextLabel.font = [UIFont systemFontOfSize:15];
    contextLabel.userInteractionEnabled = YES;
 
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0 , Dev_ScreenWidth/2-10, 20)];
    nameLabel.userInteractionEnabled = YES;
    
    nameLabel.text = dic[@"user_name"];
    
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(Dev_ScreenWidth/2-10,0 , Dev_ScreenWidth/2-10, 20)];
    timeLabel.userInteractionEnabled = YES;
    timeLabel.textAlignment = NSTextAlignmentRight;
    NSString *timeString = [NSString stringWithFormat:@"%@",dic[@"submit_date"]];

    
    //时间转换
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:timeString];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strtime = [dateformatter stringFromDate:date];
    
    timeLabel.text = strtime;

    
    
    [view addSubview:nameLabel];
    [view addSubview:contextLabel];
    [view addSubview:timeLabel];
    [self.contentView addSubview:view];
    
    
}
- (void)creatMoreComment:(NSDictionary *)dic
{
    
    NSDictionary *queto = dic[@"quote"];
    
    
    CGFloat  Quoteheight = [self.class heightWithConstraintWidth:Dev_ScreenWidth-70 string:queto[@"comment"]];
    
    ContractView *QuoteView = [[ContractView alloc]initWithFrame:CGRectMake(20, 30, Dev_ScreenWidth-50,Quoteheight + 40)];
    QuoteView.attrib =queto;
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCommentView:)];
    [QuoteView addGestureRecognizer:tap];
    
    QuoteView.backgroundColor = RGBCOLOR(255, 255, 230);
    QuoteView.layer.borderColor = [UIColor grayColor].CGColor;
    QuoteView.layer.borderWidth = 1;

    
    
    UILabel *QuotecontextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,30 , Dev_ScreenWidth-70, Quoteheight)];
    QuotecontextLabel.text = queto[@"comment"];
    QuotecontextLabel.numberOfLines = 0;
    QuotecontextLabel.font = [UIFont systemFontOfSize:15];
    QuotecontextLabel.userInteractionEnabled = YES;
    
    
    UILabel *QuotenameLabel = [[UILabel alloc]initWithFrame:CGRectMake(5,0 , Dev_ScreenWidth/2-15, 20)];
    QuotenameLabel.userInteractionEnabled = YES;
    
    QuotenameLabel.text = queto[@"user_name"];
    
    
    UILabel *QuotetimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(QuoteView.frame.size.width/2,0 , QuoteView.frame.size.width/2-5, 20)];
    QuotetimeLabel.userInteractionEnabled = YES;
    QuotetimeLabel.textAlignment = NSTextAlignmentRight;
    NSString *timeString = [NSString stringWithFormat:@"%@",queto[@"submit_date"]];
    
    
    //时间转换
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:timeString];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strtime = [dateformatter stringFromDate:date];
    
    QuotetimeLabel.text = strtime;
    
    
    
    [QuoteView addSubview:QuotenameLabel];
    [QuoteView addSubview:QuotecontextLabel];
    [QuoteView addSubview:QuotetimeLabel];

    

    
    
    
    CGFloat  height = [self.class heightWithConstraintWidth:Dev_ScreenWidth-40 string:dic[@"comment"]];
    
    CGFloat widthw = height+ QuoteView.frame.size.height+10;

    ContractView *view = [[ContractView alloc]initWithFrame:CGRectMake(10, 0, Dev_ScreenWidth-20,widthw + 20)];
    view.attrib =dic;
    

    
    UITapGestureRecognizer *tapview = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCommentView:)];
    [view addGestureRecognizer:tapview];
    
    UILabel *contextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,QuoteView.frame.size.height+40, Dev_ScreenWidth-40, height)];
    contextLabel.text = dic[@"comment"];
    contextLabel.numberOfLines = 0;
    contextLabel.font = [UIFont systemFontOfSize:15];
    contextLabel.userInteractionEnabled = YES;
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0 , Dev_ScreenWidth/2-10, 20)];
    nameLabel.userInteractionEnabled = YES;
    
    nameLabel.text = dic[@"user_name"];
    
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(Dev_ScreenWidth/2-10,0 , Dev_ScreenWidth/2-10, 20)];
    timeLabel.userInteractionEnabled = YES;
    timeLabel.textAlignment = NSTextAlignmentRight;
    NSString *timeString0 = [NSString stringWithFormat:@"%@",dic[@"submit_date"]];
    
    
    //时间转换
    NSDateFormatter *dateformatter0 = [[NSDateFormatter alloc] init];
    [dateformatter0 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date0 = [dateformatter0 dateFromString:timeString0];
    [dateformatter0 setDateFormat:@"yyyy-MM-dd"];
    NSString *strtime0 = [dateformatter0 stringFromDate:date0];
    
    timeLabel.text = strtime0;
    
    
    
    [view addSubview:nameLabel];
    [view addSubview:contextLabel];
    [view addSubview:timeLabel];
    [view addSubview:QuoteView];
    [self.contentView addSubview:view];
    
    
}



+ (CGFloat)heightWithConstraintWidth:(CGFloat)width string:(NSString *)string
{
    CGSize sizeToFit = [string sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    return sizeToFit.height;
}




+ (CGFloat)heightOfCommentWithAttributes:(NSDictionary *)att withLastHeight:(CGFloat)lastHeight lastWidth:(CGFloat)lastWidth
{
    
    NSDictionary *queto = att[@"quote"];
    if ([queto isEqual:[NSNull null]]){
        
        CGFloat  height = [self.class heightWithConstraintWidth:Dev_ScreenWidth-40 string:att[@"comment"]]+60;
        
        return  height;
    } else {
        
        CGFloat  height = [self.class heightWithConstraintWidth:Dev_ScreenWidth-40 string:att[@"comment"]]+40;
        CGFloat  Quoteheight = [self.class heightWithConstraintWidth:Dev_ScreenWidth-70 string:queto[@"comment"]]+60+height;
    
        return Quoteheight;
    }

    
   
    
}



- (void)tapCommentView:(UITapGestureRecognizer *)tap
{
    ContractView *view = (ContractView *) tap.view;
    
 
    NSLog(@"----点击哪一行---->%@",view.attrib);
     // view.attrib=
    
     NSDictionary *dic = view.attrib;
    
    
    [self CommtenViewSetTextTitle:[dic[@"id"] stringValue]];
    
}

#pragma mark 回复评论 回调刷新 评论列表
-(void)showTableview
{
    [self.tableview categoryListRequestWithObjectID:self.tableview.commModel.id_massge];
}



-(void)CommtenViewSetTextTitle:(NSString *)id_quote
{
    CommentView  *commeview = [[CommentView alloc]initWithViewComment:CGRectMake(0, 0, Dev_ScreenWidth, Dev_ScreenHeight) conmmid:self.id_massge];
    
    commeview.delegaecomme = self;
    commeview.id_quote =id_quote;
    
    [[UIApplication sharedApplication].keyWindow addSubview:commeview];
}


//
//
//+ (CGFloat)heightOfCommentWithAttributes:(NSDictionary *)att withLastHeight:(CGFloat)lastHeight lastWidth:(CGFloat)lastWidth
//{
//    NSString *comment = [NSString stringWithFormat:@"%@",att[@"comment"]];
//    
//    CGFloat height = [self.class heightWithConstraintWidth:lastWidth - 4 string:comment];
//    
//    
//    CGFloat resultHeight = lastHeight + 40 + height;
//    
//    
//    NSDictionary *queto = att[@"quote"];
//    if ([queto isEqual:[NSNull null]]){
//        return resultHeight;
//    } else {
//        return [self.class heightOfCommentWithAttributes:queto withLastHeight:resultHeight lastWidth:lastWidth - 4];
//    }
//    
//}
//
//
//
//+ (CGFloat)heightWithConstraintWidth:(CGFloat)width string:(NSString *)string
//{
//    CGSize sizeToFit = [string sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
//    return sizeToFit.height;
//}
//
//- (void)addCommentViewWith:(NSDictionary *)att view:(ContractView *)view isFirst:(BOOL)isFirst
//{
//    
//    
//    view.attrib = att;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCommentView:)];
//    [view addGestureRecognizer:tap];
//    
//    
//    if (!isFirst) {
//        view.backgroundColor = [UIColor yellowColor];
//        view.layer.borderColor = [UIColor grayColor].CGColor;
//        view.layer.borderWidth = 1;
//    }
//    
//    NSString *content = [NSString stringWithFormat:@"%@",att[@"comment"]];
//    CGFloat contentH = [self.class heightWithConstraintWidth:view.frame.size.width  string:content];
//    
//    CGFloat contectX = isFirst ? 30 :10;
//    
//    UILabel *contextLabel = [[UILabel alloc]initWithFrame:CGRectMake(contectX, view.size.height - contentH - 5, view.frame.size.width, contentH)];
//    contextLabel.text = att[@"comment"];
//    contextLabel.numberOfLines = 0;
//    contextLabel.font = [UIFont systemFontOfSize:15];
//    contextLabel.userInteractionEnabled = YES;
//    
//    
//    CGFloat nameLabelY = isFirst ? 7:view.frame.size.height - contextLabel.frame.size.height - contentH - 8;
//    
//    CGFloat nameLabelX = isFirst ? 70:2;
//    
//    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabelX, nameLabelY, view.frame.size.width - 3, 17)];
//    nameLabel.userInteractionEnabled = YES;
//    
//    nameLabel.text = att[@"user_name"];
//    
//    
//    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(view.size.width - 150 - 10,nameLabel.frame.origin.y, 150, 17)];
//    timeLabel.userInteractionEnabled = YES;
//    timeLabel.textAlignment = NSTextAlignmentRight;
//    NSString *timeString = [NSString stringWithFormat:@"%@",att[@"submit_date"]];
//    
//    
//    //时间转换
//    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
//    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *date = [dateformatter dateFromString:timeString];
//    [dateformatter setDateFormat:@"HH:mm:ss"];
//    NSString *strtime = [dateformatter stringFromDate:date];
//    
//    timeLabel.text = strtime;
//    
//    
//    if (isFirst) {
//        UIImageView *userImgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, 30, 30)];
//        userImgView.image = [UIImage imageNamed:@"action_login"];
//        [view addSubview:userImgView];
//    }
//    
//    [view addSubview:nameLabel];
//    [view addSubview:contextLabel];
//    [view addSubview:timeLabel];
//    
//    NSDictionary *queto = att[@"quote"];
//    if ([queto isEqual:[NSNull null]])
//    {
//        return;
//    }
//    if (queto) {
//        CGFloat heghtt = [self.class heightOfCommentWithAttributes:queto withLastHeight:0 lastWidth:view.frame.size.width];
//        
//        CGFloat x = isFirst ? 30:3;
//        CGFloat w = isFirst ? view.frame.size.width - 60:view.frame.size.width - 5;
//        
//        CGFloat subViewY = isFirst ? 37:2;
//        
//        ContractView *subView = [[ContractView alloc]initWithFrame:CGRectMake(x, subViewY, w,heghtt)];
//        [self addCommentViewWith:queto view:subView isFirst:NO];
//        [view addSubview:subView];
//    }
//    
//    
//}
//
//- (void)tapCommentView:(UITapGestureRecognizer *)tap
//{
//    ContractView *view = (ContractView *) tap.view;
//    
//    if (self.tapBlock) {
//        self.tapBlock(view.attrib);
//    }
//}
//
//
//- (void)tapCommentViewWithBlock:(void(^)(NSDictionary *att))block;
//{
//    if (block) {
//        self.tapBlock = block;
//    }
//}
//




@end
